// 表单验证

let common = {
	checkRules(params, rules) {
		for (let key in rules) {
			let arr = rules[key]
			for (let i = 0; i < arr.length; i++) {
				let rule = arr[i]
				let val = params[key]===0 ? '0' : params[key] || ''
				if (rule.required && val === '') { // 非空判定
					uni.showToast({
						title: rule.message,
						icon: 'none'
					})
					return false
				}
				if (rule.pattern && !rule.pattern.test(val)) { // 正则判定
					uni.showToast({
						title: rule.message,
						icon: 'none'
					})
					return false
				}
				if (val.length < rule.min) { // 长度的判定
					uni.showToast({
						title: rule.message,
						icon: 'none'
					})
					return false
				}
			}
		}
		return true
	}
}

export default common